//
//  BGNewFeatureCell.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/24.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit

class BGNewFeatureCell: UICollectionViewCell {

    var imageIndex: Int = 0 {
        didSet{
            imageView.image = UIImage(named: "new_feature_\(imageIndex+1)")
            //防止重用特意添加
            startButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
        self.setupLayout()
    }
    
    private func setupUI() {
        backgroundColor = RandomColor()
        //添加控件
        contentView.addSubview(imageView)
        contentView.addSubview(startButton)
    }
    
    private func setupLayout() {
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        startButton.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showStartButton() {
        self.startButton.hidden = false
        //缩小
        self.startButton.transform = CGAffineTransformMakeScale(0, 0)
        self.startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1, delay: 0.3, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: {
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) in
                self.startButton.userInteractionEnabled = true
        }
        
    }
    
    //MARK: - 监听事件
    @objc private func didClickButton() {
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "newFeature")
    }
    
    //MARK: - 懒加载
    private lazy var imageView: UIImageView = UIImageView()
    
    private lazy var startButton: UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(didClickButton), forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setTitle("立即体验", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(20)
        
        btn.hidden = true
        btn.sizeToFit()
        
        return btn
    }()
    
}
